package com.hhl.bookstore.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hhl.bookstore.dao.CategoryDao;
import com.hhl.bookstore.domain.Category;
import com.hhl.bookstore.exception.DaoException;
import com.hhl.bookstore.util.JdbcUtil;

public class CategoryDaoMySQLImpl implements CategoryDao {

	@Override
	public void add(Category category) {
		
		if(category==null)
			throw new IllegalArgumentException();
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		
		try{
			conn = com.hhl.bookstore.util.JdbcUtil.getConnection();
			
			
			
			stmt = conn.prepareStatement("insert into category (id,name,description) values(?,?,?)");
			stmt.setString(1, category.getId());
			stmt.setString(2, category.getName());
			stmt.setString(3, category.getDescription());
			stmt.executeUpdate();
		}catch(Exception e){
			try {
				throw new DaoException(e);
			} catch (DaoException e1) {
				e1.printStackTrace();
			}
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
		
	}

	@Override
	public void update(Category category) {
		

		if(category==null)
			throw new IllegalArgumentException();
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		

		try{
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("update category set name=?,description=? where id=?");
			
			stmt.setString(1, category.getName());
			stmt.setString(2, category.getDescription());
			stmt.setString(3, category.getId());
			stmt.executeUpdate();
		}catch(Exception e){
			try {
				throw new DaoException(e);
			} catch (DaoException e1) {
				e1.printStackTrace();
			}
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
		
	}

	@Override
	public void delete(String categoryId) {
		


		if(categoryId==null)
			throw new IllegalArgumentException();
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		

		try{
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("delete from category where id=?");
			
			stmt.executeUpdate();
		}catch(Exception e){
			try {
				throw new DaoException(e);
			} catch (DaoException e1) {
				e1.printStackTrace();
			}
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
		
		
	}

	
	@Override
	public Category findOne(String categoryId) {
		
		if(categoryId==null)
			throw new IllegalArgumentException();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try{
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("select id,name,description from category where id=?");
			stmt.setString(1, categoryId);
			rs = stmt.executeQuery();
			if(rs.next()){
				Category c = new Category();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setDescription(rs.getString("description"));
				return c;
			}else
				return null;
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
	}
		

	@Override
	public List<Category> findAll() {
		

		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Category> cs = new ArrayList<Category>();
		try{
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement("select id,name,description from category");
			rs = stmt.executeQuery();
			while(rs.next()){
				Category c = new Category();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setDescription(rs.getString("description"));

				cs.add(c);
			}
				return cs;
		}catch(Exception e){
			try {
				throw new DaoException(e);
			} catch (DaoException e1) {
				e1.printStackTrace();
			}
		}finally{
			JdbcUtil.release(rs, stmt, conn);
		}
		return cs;
	}

}
